package com.kfy.databinding3;

import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.squareup.picasso.Picasso;

/**
 * @ProjectName: JetpackDemo
 * @Package: com.kfy.databinding3
 * @Author: K.F.Y
 * @Date: 2022/4/27
 */
public class ImageViewBindingAdapter {

    @BindingAdapter("image")
    public static void setImage(ImageView imageView, String url){
        Log.d("KFYTEST","url:" + url);
        if (!TextUtils.isEmpty(url)) {
            Picasso.get()
                    .load(url)
                    .placeholder(R.drawable.ic_launcher_background)
                    .into(imageView);
        }
    }

    @BindingAdapter("image")
    public static void setImage(ImageView imageView, int resId){
        Log.d("KFYTEST","resId:" + resId);
        imageView.setImageResource(resId);
    }

    @BindingAdapter(value = {"image","defaultImage"},requireAll = false)
    public static void setImage(ImageView imageView, String url, int resId){
        if (!TextUtils.isEmpty(url)) {
            Picasso.get()
                    .load(url)
                    .placeholder(R.drawable.ic_launcher_background)
                    .into(imageView);
        } else {
            imageView.setImageResource(resId);
        }
    }

}
